<template>
  <div class="form-container">
    <el-form 
      :model="formData" 
      :rules="formRules" 
      ref="adminForm" 
      label-width="100px"
      class="admin-form"
    >
      <el-form-item label="ID">
        <el-input v-model="formData.id" readonly></el-input>
      </el-form-item>
      
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      
      <el-form-item label="密码" prop="password">
        <el-input 
          v-model="formData.password" 
          type="password"
          show-password
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="formData.gender">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item label="联系电话" prop="telephone">
        <el-input 
          v-model.number="formData.telephone" 
          placeholder="请输入联系电话"
        ></el-input>
      </el-form-item>
      
      <el-form-item>
        <el-button 
          type="primary" 
          @click="handleSubmit"
          :loading="isSubmitting"
        >
          保存修改
        </el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import dormitoryAdminApi from '@/api/dormitoryAdmin'

export default {
  name: 'DormitoryAdminEdit',
  data() {
    return {
      isSubmitting: false,
      formData: {
        id: '',
        username: '',
        password: '',
        name: '',
        gender: '男',
        telephone: ''
      },
      formRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在3到20个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少6位', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.fetchAdminData()
  },
  methods: {
    // 获取宿管数据
    async fetchAdminData() {
      const id = this.$route.query.id
      if (!id) {
        this.$message.warning('缺少ID参数')
        return this.$router.push('/dormitoryAdminManager')
      }

      try {
        const response = await dormitoryAdminApi.getDormitoryAdminById(id)
        if (response.data.code === 0) {
          this.formData = { ...response.data.data }
          // 设置默认性别
          this.formData.gender = this.formData.gender || '男'
        } else {
          this.$message.error(response.data.msg || '获取宿管信息失败')
          this.$router.push('/dormitoryAdminManager')
        }
      } catch (error) {
        console.error('获取宿管信息失败:', error)
        this.$message.error('获取宿管信息失败')
        this.$router.push('/dormitoryAdminManager')
      }
    },
    
    // 提交表单
    async handleSubmit() {
      try {
        const valid = await this.$refs.adminForm.validate()
        if (!valid) return
        
        this.isSubmitting = true
        const response = await dormitoryAdminApi.updateDormitoryAdmin(this.formData)
        
        if (response.data.code === 0) {
          this.$message.success('宿管信息更新成功')
          this.$router.push('/dormitoryAdminManager')
        } else {
          this.$message.error(response.data.msg || '更新失败')
        }
      } catch (error) {
        console.error('更新宿管信息失败:', error)
        this.$message.error('更新失败，请稍后重试')
      } finally {
        this.isSubmitting = false
      }
    },
    
    // 重置表单
    handleReset() {
      this.$refs.adminForm.resetFields()
      // 保留ID字段
      const id = this.formData.id
      this.$nextTick(() => {
        this.formData.id = id
      })
    }
  }
}
</script>

<style scoped>
.form-container {
  max-width: 600px;
  margin: 20px auto;
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.admin-form {
  padding: 20px;
}
</style>